﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System.Collections.Generic;
using System.Windows.Controls;
using BrainTechLLC.ThreadSafeObjects;
using ImageAppViewModels;
using System;
using System.Diagnostics;

namespace WpfImageDuplicates
{
    public partial class ImageCollectionView : UserControl
    {
        public ThreadSafeQueue<FileImageViewModel> _itemsAdded = new ThreadSafeQueue<FileImageViewModel>();
        
        public void AddListItems(List<FileImageViewModel> vms)
        {
            foreach (var vm in vms)
            {
                AddListItem(vm);
            }
        }

        public void AddListItem(FileImageViewModel vm)
        {
            Debug.Assert(_itemsAdded.Count < 10000);                
            _itemsAdded.Enqueue(vm);
        }

        public void RemoveListItem(FileImageViewModel vm)
        {
            for (int n = Displayed.Count - 1; n >= 0; n--)
            {
                var vmItem = Displayed[n] as FileImageViewModel;
                if (vm == vmItem)
                {
                    ThumbsHandler.ForceRemoveThumbView(vmItem.FileImage);
                    Displayed.RemoveAt(n);
                    Displayed.IndexDirty = true;
                    break;
                }
            }
        }

        public void RemoveListItem(IList<FileImageViewModel> vms)
        {
            for (int n = Displayed.Count - 1; n >= 0; n--)
            {
                var vmItem = Displayed[n] as FileImageViewModel;

                for (int i = vms.Count - 1; i >= 0; i--)
                {
                    if (vms[i] == vmItem)
                    {
                        Displayed.RemoveAt(n);
                        ThumbsHandler.ForceRemoveThumbView(vmItem.FileImage);
                        vms.RemoveAt(i);
                        break;
                    }
                }
            }
        }

        public void ClearListItems()
        {
            Displayed.Clear();
        }
    }
}